package com.cdn.nodes.service.impl;

import com.cdn.common.core.utils.StringUtils;
import com.cdn.dns.api.RemoteDnsService;
import com.cdn.nodes.domain.EdgeClusters;
import com.cdn.nodes.domain.EdgeNodeSsh;
import com.cdn.nodes.domain.EdgeSshCluster;
import com.cdn.nodes.domain.dto.EdgeClustersAddDTO;
import com.cdn.nodes.domain.vo.EdgeClustersDnsVO;
import com.cdn.nodes.mapper.*;
import com.cdn.nodes.service.IEdgeClustersService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.rmi.ServerException;
import java.util.List;

/**
 * 边缘集群，存储边缘集群的相关信息，包括配置和状态Service业务层处理
 * 
 * @author Cinzia
 * @date 2024-12-14
 */
@Service
public class EdgeClustersServiceImpl implements IEdgeClustersService 
{
    @Resource
    private EdgeClustersMapper edgeClustersMapper;
    @Resource
    private EdgeSshClusterMapper edgeSshClusterMapper;
    @Resource
    private EdgeClustersNodesMapper edgeClustersNodesMapper;
    @Resource
    private EdgeNodeSshMapper edgeNodeSshMapper;
    @Resource
    private RemoteDnsService remoteDnsService;
    /**
     * 查询边缘集群，存储边缘集群的相关信息，包括配置和状态
     * 
     * @param clusterId 边缘集群，存储边缘集群的相关信息，包括配置和状态主键
     * @return 边缘集群，存储边缘集群的相关信息，包括配置和状态
     */
    @Override
    public EdgeClusters selectEdgeClustersByClusterId(Long clusterId)
    {
        return edgeClustersMapper.selectEdgeClustersByClusterId(clusterId);
    }

    /**
     * 查询边缘集群，存储边缘集群的相关信息，包括配置和状态列表
     * 
     * @param edgeClusters 边缘集群，存储边缘集群的相关信息，包括配置和状态
     * @return 边缘集群，存储边缘集群的相关信息，包括配置和状态
     */
    @Override
    public List<EdgeClusters> selectEdgeClustersList(EdgeClusters edgeClusters)
    {
        return edgeClustersMapper.selectEdgeClustersList(edgeClusters);
    }

    /**
     * 新增边缘集群，存储边缘集群的相关信息，包括配置和状态
     * 
     * @param edgeClustersAddDTO 边缘集群，存储边缘集群的相关信息，包括配置和状态
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEdgeClusters(EdgeClustersAddDTO edgeClustersAddDTO)
    {
        EdgeClusters edgeClusters = new EdgeClusters();
        BeanUtils.copyProperties(edgeClustersAddDTO, edgeClusters);

        int result = edgeClustersMapper.insertEdgeClusters(edgeClusters);
        if (result > 0) {
            Long clusterId = edgeClusters.getClusterId();

            EdgeSshCluster sshCluster = new EdgeSshCluster();
            sshCluster.setClusterId(clusterId);
            sshCluster.setSshId(edgeClustersAddDTO.getSelectedAuthId());

            // 插入sshCluster到中间表
            edgeSshClusterMapper.insertEdgeSshCluster(sshCluster);
        }

        remoteDnsService.clusterBindProviders(edgeClusters.getClusterId(),edgeClustersAddDTO.getProviderId());
        return result;
    }

    /**
     * 修改边缘集群，存储边缘集群的相关信息，包括配置和状态
     * 
     * @param edgeClusters 边缘集群，存储边缘集群的相关信息，包括配置和状态
     * @return 结果
     */
    @Override
    public int updateEdgeClusters(EdgeClusters edgeClusters)
    {
        return edgeClustersMapper.updateEdgeClusters(edgeClusters);
    }

    /**
     * 批量删除边缘集群，存储边缘集群的相关信息，包括配置和状态
     * 
     * @param clusterIds 需要删除的边缘集群，存储边缘集群的相关信息，包括配置和状态主键
     * @return 结果
     */
    @Override
    public int deleteEdgeClustersByClusterIds(Long[] clusterIds)
    {
        // 删除集群和节点ssh关联表
        edgeSshClusterMapper.deleteEdgeSshClusterByClusterId(clusterIds);
        // 删除集群和服务商关联表
        remoteDnsService.delClusterBindProviders(clusterIds);
        return edgeClustersMapper.deleteEdgeClustersByClusterIds(clusterIds);
    }

    /**
     * 删除边缘集群，存储边缘集群的相关信息，包括配置和状态信息
     * 
     * @param clusterId 边缘集群，存储边缘集群的相关信息，包括配置和状态主键
     * @return 结果
     */
    @Override
    public int deleteEdgeClustersByClusterId(Long clusterId)
    {
        return edgeClustersMapper.deleteEdgeClustersByClusterId(clusterId);
    }

    @Override
    public List<Long> getByClusterId(Long cluster) throws ServerException {
        if (StringUtils.isNull(cluster)){
            throw new ServerException("集群ID为空...");
        }
        return edgeClustersNodesMapper.selectNodeIdsByClusterId(cluster);
    }

    @Override
    public String getDnsByClusterId(Long cluster) throws ServerException {
        if (StringUtils.isNull(cluster)){
            throw new ServerException("集群ID为空...");
        }
        return edgeClustersMapper.selectDnsByClusterId(cluster);
    }

    @Override
    @Transactional(rollbackFor = ServerException.class)
    public String getClusterSsh(Long cluster) {
        // 根据集群Id获取sshId
        Long sshId = edgeSshClusterMapper.selectEdgeSshClusterByCluster(cluster);
        // 根据sshId获取ssh密钥
        EdgeNodeSsh edgeNodeSsh = edgeNodeSshMapper.selectEdgeNodeSshById(sshId);
        return edgeNodeSsh.getSshKey();
    }

    @Override
    public List<EdgeClustersDnsVO> getClustersDnsInfo(String keyword) {
        // 根据集群获取域名信息
        return edgeClustersMapper.getClustersDnsInfo(keyword);
    }

    @Override
    public EdgeClustersDnsVO getClustersDnsInfoByClusterId(Long clusterId) {
        // 根据集群Id获取域名信息
        return edgeClustersMapper.getClustersDnsInfoByClusterId(clusterId);
    }

    @Override
    public String getClusterByNodeId(Long nodeId) {
        return edgeClustersMapper.getClusterByNodeId(nodeId);
    }

    @Override
    public String getInstallPath(Long nodeId) {
        return edgeClustersMapper.getInstallPath(nodeId);
    }


}
